package com.ou.csbs.capstone.models.shapes;

import java.util.ArrayList;
import java.util.Iterator;

import com.ou.csbs.capstone.models.AttributeModel;
import com.ou.csbs.capstone.models.RenderableModel;
import com.ou.csbs.capstone.views.worldwind.WorldWindCanvas;

import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Renderable;
import gov.nasa.worldwind.render.SurfaceCircle;
import gov.nasa.worldwind.util.measure.LengthMeasurer;
/**
 * A drawn circle object.
 * It is extended off of a SurfaceCircle so that any picking that occurs during the EditingTool process does not interfere with existing SurfaceCircles.
 * Secondly, the constructor and certain methods have been added to aid in object creation, in adherence to how the drawing tool is designed to work.
 * 
 * @author Senior Capstone
 */
public class Circle extends SurfaceCircle{
	BasicShapeAttributes attr;
	
	public Circle(LatLon center, Position edge){
		super(AttributeModel.getAttributes(), center, calculateRadius(center, edge));
	}
	public Circle(BasicShapeAttributes bsa, LatLon center, Position edge){
		super(bsa, center, calculateRadius(center, edge));
	}

	public void drawShape(RenderableModel model) {
		model.getDrawingLayer().addRenderable(this);
		WorldWindCanvas.getWorldWindow().redrawNow();
	}
	public void removeShape(RenderableModel model){
		model.getDrawingLayer().removeRenderable(this);
		WorldWindCanvas.getWorldWindow().redrawNow();
	}
	public boolean isDrawn(RenderableModel model) {
		Iterator<Renderable> it =  model.getDrawingLayer().getRenderables().iterator();
		while(it.hasNext()){
			if(it.next().equals(this))
				return true;
		}
		return false;
	}
	public void updateRadius(Position newPointOnEdge){
		this.setRadius(calculateRadius(this.getCenter(), newPointOnEdge));
	}
	public static double calculateRadius(LatLon center, Position edge){
		ArrayList<Position> radiusPoints = new ArrayList<Position>();
		radiusPoints.add((Position) center);
		radiusPoints.add((Position) edge);
		LengthMeasurer lm = new LengthMeasurer(radiusPoints);
		double radius = lm.getLength(WorldWindCanvas.getWorldWindModel().getGlobe());
		if(radius < 0)
			return 0;
		return radius;
	}
	
}
